package com.summer.framework.core.i18n;

import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;

/**
 * Description: 多语言国际化配置
 * @auther Ice Yang
 * Date 2025/7/2 13:53
 */
@Configuration
@AutoConfigureBefore({WebMvcAutoConfiguration.class, MessageSourceAutoConfiguration.class})
public class I18nConfig implements WebMvcConfigurer {
    /**
     * 默认国际化解析器
     * @return
     */
    @Bean
    public LocaleResolver localeResolver() {
        return new SummerLocalResolver();
    }

    /**
     * 配置并创建一个本地化变更拦截器 bean
     * 该拦截器用于根据请求参数确定用户希望使用的语言环境
     *
     * @return LocaleChangeInterceptor 本地化变更拦截器实例，用于拦截并处理语言环境变更请求
     */
    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
        // 指定切换语言的参数名
        lci.setParamName("lang");
        return lci;
    }

    /**
     * 添加拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(localeChangeInterceptor());
    }

//    @Bean
//    public MessageSource messageSource() {
//
//    }
}
